//variable
var orgArrayAm;

//create function
function createTextBox(name, value, readOnly) {
	var txtBox = jQuery("<input type=\"text\"/>");
	txtBox.attr("name", name);
	txtBox.attr("id", name);
	txtBox.val(value);
	if(readOnly)
		txtBox.attr("readonly", "readonly");
	return txtBox;
}

function createHidden(name, value) {
	var hidden = jQuery("<input type=\"hidden\" />");
	hidden.attr("name", name);
	hidden.attr("id" ,name);
	hidden.val(value);
	return hidden;
}

function createFile(name) {
	var inputFile = jQuery("<input type=\"file\" />");
	inputFile.attr("name", name);
	inputFile.attr("id", name);
	return inputFile;
}

//check function
function checkName(event) {
	var inputName = jQuery(this).val();
	var errTag = jQuery("span[name='err_"+ jQuery(this).attr("name")+ "']");
	if(inputName == null || inputName.length < 1 || inputName.length > 50)
		errTag.text("Incorrect input");
	else {
		errTag.text("");
	}
}

//clone array
function cloneArray(arrAm) {
	if(arrAm == null || arrAm.length < 1)
		return new Array();
	
	var newArr = new Array();
	for(var i = 0; i < arrAm.length; i++) {
		newArr[i] = new Object();
		newArr[i].order = arrAm[i].order;
		newArr[i].name = arrAm[i].name;
		newArr[i].fileName = arrAm[i].fileName;
		newArr[i].info = arrAm[i].info;
		newArr[i].oldOrder = arrAm[i].order;
	}
	return newArr;
}

//form function
function restartForm() {
	arrayAm = cloneArray(orgArrayAm);
	drawTable();
}

function addBeforeThis(index) {
	saveToArr();
	
	var am = new Object();
	am.order = index;
	am.name = "New Assignment Material";
	am.info = "";
	am.fileName = "";
	am.oldOrder = -1;
	arrayAm.splice(index, 0, am);
	
	resetOrder();
	drawTable();
}

function addAtEnd() {
	saveToArr();
	
	var am = new Object();
	am.order = -1;
	am.name = "New Assignment Material";
	am.info = "";
	am.fileName = "";
	am.oldOrder = -1;
	arrayAm.push(am);

	resetOrder();
	drawTable();
}

function removeThis(index) {
	if(arrayAm.length < 2)
		arrayAm = new Array();
	saveToArr();
	arrayAm.splice(index, 1);
	
	resetOrder();
	drawTable();
}

//ms array process
function resetOrder() {
	for(var i = 0; i < arrayAm.length; i++)
		arrayAm[i].order = i + 1;
}

//if any error, return true
function checkAll() {
	resetOrder();
	for(var i = 0; i < arrayAm.length; i++) {
		 var tmp = arrayAm[i].name;
		 if(tmp == null || tmp.length < 1 || tmp.length > 50)
			 return i + 1;
		 if(arrayAm[i].info == null)
			 arrayAm[i].info = "";
		 if(arrayAm[i].fileName == null)
			 arrayAm[i].fileName = "";
		 if(arrayAm[i].oldOrder == null)
			 arrayAm[i].oldOrder = -1;
	}
	return -1;
}

//draw table function
function drawTable() {
	
	//draw to table
	var amTable = jQuery("table#am_table");
	//clear the old data
	var intr = amTable.children('tbody').children('tr');
	if (intr.length > 1)
		intr.next().remove();
		
	//draw data
	for(var i = 0; i < arrayAm.length; i++) {
		var aRow = jQuery("<tr/>").appendTo(amTable);
		var ltd = jQuery("<td/>").appendTo(aRow);
		
		//inner table @ left td
		var innerTb = jQuery("<table width='100%' />").appendTo(ltd);
		//name
		var innRow = jQuery("<tr />").appendTo(innerTb);
		jQuery("<td>Name:</td>").appendTo(innRow);
		inntd = jQuery("<td />").appendTo(innRow);
		createTextBox(("name_"+ (i+1)), arrayAm[i].name, false).appendTo(inntd);
		if(arrayAm[i].oldOrder > 0) {
			jQuery(" <span>("+ arrayAm[i].oldOrder+ ")</span>").appendTo(inntd);
			createHidden(("order_")+ (i+1), arrayAm[i].oldOrder).appendTo(inntd);
		}
		jQuery("<span name=\"err_name_"+ (i+1)+ "\" style='color:red;'/>").appendTo(inntd);
		//info
		innRow = jQuery("<tr />").appendTo(innerTb);
		jQuery("<td>Information:</td>").appendTo(innRow);
		inntd = jQuery("<td />").appendTo(innRow);
		//textarea for info
		var innTarea = jQuery("<textarea rows=\"5\" cols=\"40\"/>");
		innTarea.attr("name", "info_" + (i+1));
		innTarea.attr("id", "info_" + (i+1));
		innTarea.val(arrayAm[i].info);
		innTarea.appendTo(inntd);
		//File
		innRow = jQuery("<tr />").appendTo(innerTb);
		jQuery("<td>File:</td>").appendTo(innRow);
		inntd = jQuery("<td />").appendTo(innRow);
		if(arrayAm[i].fileName != null && arrayAm[i].fileName.lastIndexOf('.') > -1) {
			var dlLink = jQuery("<a href='f_download_material.jsp?ac_id="+ acId+ "&am_order="+ arrayAm[i].oldOrder+ "'><img src='../images/button/download.png' class='img_icon' alt='Download Now!'/>"+ arrayAm[i].fileName+ "</a>");
			dlLink.appendTo(inntd);
			createHidden(("file_"+ (i+1)), arrayAm[i].fileName).appendTo(inntd);
		} else {
			createFile("file_"+ (i+1)).appendTo(inntd);
		}
			
		//Functions
		var ftd = jQuery("<td />").appendTo(aRow);
		jQuery('<a href="javascript:addBeforeThis('+ i+ ');">Add Before This</a><br />').appendTo(ftd);
		jQuery('<a href="javascript:removeThis('+ i+ ');">Remove This</a>').appendTo(ftd);
	}
	//add function link @ the end of table
	var lastRow = jQuery("<tr/>").appendTo(amTable);
	var lastCol = jQuery("<td colspan=\"2\"/>").appendTo(lastRow);
	jQuery('<a href="javascript:addAtEnd();">Add New Scheme</a><br />').appendTo(lastCol);
	jQuery('<a href="javascript:restartForm();">Undo All change</a>').appendTo(lastCol);
	//add change event
	jQuery('input[name^="name_"]').change(checkName);
	
	
}

function saveToArr() {
	//get name
	var msVal = jQuery("input[name^=\"name_\"]");
	jQuery.each(msVal, function() {
		var name = jQuery(this).val();
		var index = parseInt(jQuery(this).attr("name").substring(5)) - 1;
		if(index >= 0 && index < arrayAm.length)
			arrayAm[index].name = name;
	});
	//get criteria
	msVal = jQuery("textarea[name^=\"info_\"]");
	jQuery.each(msVal, function() {
		var info = jQuery(this).val();
		var index = parseInt(jQuery(this).attr("name").substring(5)) - 1;
		if(index >= 0 && index < arrayAm.length)
			arrayAm[index].info = info;
	});
}

jQuery(document).ready(function() {
	if(arrayAm == null || arrayAm.length < 1)
		arrayAm = new Array();
	orgArrayAm = arrayAm;
	arrayAm = cloneArray(orgArrayAm);
	drawTable();
	
	jQuery("#am_form").submit(function(event) {
		var isSubmit = true;
		jQuery('input[name^="name_"]').change();
		jQuery("span[name^='err']").each(function() {
			var txt = jQuery(this).text();
			if(txt.indexOf("Incorrect") > -1) {
				isSubmit = false;
				return false;
			}
		});
		return isSubmit;
	});
});